package com.lill.workflow.web;

import java.util.List;
import java.util.Map;

import com.lill.common.component.util.IdUtil;
import com.lill.workflow.service.WorkflowStepHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lill.common.component.util.PageUtils;
import com.lill.common.component.util.Query;
import com.lill.common.component.util.R;
import com.lill.support.annotation.CurrentUser;
import com.lill.system.constant.SystemConstant;
import com.lill.system.model.SysUserEntity;
import com.lill.workflow.model.WorkflowCurrentModel;
import com.lill.workflow.service.IWorkflowCurrentService;

/**
 * Controller
 *
 * @author lill
 * @date 2019-05-26 12:05:20
 */
@RestController
@RequestMapping("workflow/current")
public class WorkflowCurrentController {
    @Autowired
    private IWorkflowCurrentService workflowCurrentService;
    @Autowired
    private WorkflowStepHistoryService workflowStepHistoryService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,@CurrentUser SysUserEntity curUser) {
    	if (!SystemConstant.SUPER_ADMIN.equals(curUser.getId())) {
    		params.put("companyId", curUser.getCompanyId());
    	}
        //查询列表数据
        Query query = new Query(params);

        List<WorkflowCurrentModel> workflowCurrentList = workflowCurrentService.queryList(query);
        int total = workflowCurrentService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(workflowCurrentList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
        WorkflowCurrentModel workflowCurrent = workflowCurrentService.queryObject(id);
        return R.ok().put("workflowCurrent", workflowCurrent);
    }

    /**
     * 查看当前审核人信息
     */
    @RequestMapping("/employee")
    public R employee(@RequestParam Map<String, Object> params,@CurrentUser SysUserEntity user) {
        // 如果不是超级管理员，则只能查询本部门及子部门数据
        if (!SystemConstant.SUPER_ADMIN.equals(user.getId())) {
            //branchId不为空   代表公司
            if (user.getBranchId() != null && !"".equals(user.getBranchId())) {
                params.put("branchId",user.getBranchId());
            }
            params.put("companyId", user.getCompanyId());
        }
        WorkflowCurrentModel workflowCurrent = workflowCurrentService.queryCurrent(params);
        return R.ok().put("current", workflowCurrent);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody WorkflowCurrentModel workflowCurrent) {
        workflowCurrent.setId(IdUtil.createIdbyUUID());
        int result=workflowCurrentService.save(workflowCurrent);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody WorkflowCurrentModel workflowCurrent,@CurrentUser SysUserEntity user) {
        int result=workflowCurrentService.update(workflowCurrent);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 查看根据业务Id审批记录
     */
    @RequestMapping("/approvalList")
    public R approvalList(@RequestParam Map<String, Object> params) {
        return R.ok().put("list", workflowStepHistoryService.queryListByNumber(params));
    }
}
